package DT3;

import java.text.DecimalFormat;
import javagprssrv.Common.DateTime;
import java.sql.ResultSet;
import java.util.Map;
import javagprssrv.DataBase.DBSQLExecute;
import javagprssrv.JGS_Param;
import javagprssrv.StructClass.structTask;
import javagprssrv.StructClass.structRecvData;

public class TrdUDPDataSaver extends java.lang.Thread
{
    javagprssrv.StructClass.structRecvData recvData;
    javagprssrv.StructClass.structTask task;
    int JobIndex;
    java.lang.String _Protocol;
    int _UDPServerSocketIndex;
    java.lang.String _RemoteIP;
    int _RemotePort;
    java.lang.String _Data;
    java.lang.String _OriginRecvData;
    int RecvDataType;
    java.lang.String TaskID;
    int DataType;
    java.lang.String TermNo;
    java.lang.String YBH;
    java.lang.String AsynTelNo;
    java.lang.String zyxs;
    java.lang.String csxs;
    java.lang.String cxxs;
    java.lang.String zdzs;
    java.lang.String zxzs;
    java.lang.String hgs;
    double zdz;
    double zxz;
    double pjz;
    double z00;
    double z01;
    double z02;
    double z03;
    double z04;
    double z05;
    double z06;
    double z07;
    double z08;
    double z09;
    double z10;
    double z11;
    double z12;
    double z13;
    double z14;
    double z15;
    double z16;
    double z17;
    double z18;
    double z19;
    double z20;
    double z21;
    double z22;
    double z23;
    java.lang.String sYB_BL;
    java.lang.String szdz;
    java.lang.String szxz;
    java.lang.String spjz;
    java.lang.String sz00;
    java.lang.String sz01;
    java.lang.String sz02;
    java.lang.String sz03;
    java.lang.String sz04;
    java.lang.String sz05;
    java.lang.String sz06;
    java.lang.String sz07;
    java.lang.String sz08;
    java.lang.String sz09;
    java.lang.String sz10;
    java.lang.String sz11;
    java.lang.String sz12;
    java.lang.String sz13;
    java.lang.String sz14;
    java.lang.String sz15;
    java.lang.String sz16;
    java.lang.String sz17;
    java.lang.String sz18;
    java.lang.String sz19;
    java.lang.String sz20;
    java.lang.String sz21;
    java.lang.String sz22;
    java.lang.String sz23;
    java.lang.String YB_YBH;
    double YB_BL;
    int YB_BBXSW;
    java.lang.String YB_GSMC;
    java.lang.String YB_floatGS;
    java.lang.String s2_Parameter;
    java.lang.String s2_Year;
    java.lang.String s2_Month;
    java.lang.String s2_Date;
    
    public TrdUDPDataSaver() {
        super("TrdUDPDataSaver");
        this.JobIndex = -1;
        this.RecvDataType = -1;
        this.TermNo = "";
        this.YBH = "";
        this.AsynTelNo = "";
    }
    
    private boolean findRecvData() {
        boolean res = false;
        synchronized (javagprssrv.JGS_Param.UnSavedRecvData_UDP) {
            if (javagprssrv.JGS_Param.UnSavedRecvData_UDP.size() > 0) {
                this.recvData = javagprssrv.JGS_Param.UnSavedRecvData_UDP.remove(0);
                res = true;
            }
            if (javagprssrv.JGS_Param.UnSavedRecvData_UDP.size() > 10000) {
                javagprssrv.JGS_Param.UnSavedRecvData_UDP.clear();
            }
        }
        final int firstChar = this.recvData.Data.substring(0, 1).charAt(0);
        if (firstChar != 89 && firstChar != 65) {
            this.recvData.Data = "A" + this.recvData.Data;
        }
        this._Protocol = this.recvData.Protocol.trim();
        this._RemoteIP = this.recvData.RemoteIP;
        this._RemotePort = this.recvData.RemotePort;
        this._UDPServerSocketIndex = this.recvData.UDPServerSocketIndex;
        this._Data = this.recvData.Data;
        this._OriginRecvData = this.recvData.Data;
        return res;
    }
    
	@java.lang.Override
	public void run() {
		try {
			while (true) {
				java.lang.Thread.sleep(20L);
				if (!this.findRecvData()) {
					continue;
				}
				this.DataType = this._Data.substring(4, 5).charAt(0);
				if (!this.checkData()) {
					continue;
				}
				if (this.DataType == 3) {
					this.save_03();
				}
				if (this.DataType == 4) {
					this.save_04();
				}
				if (this.DataType == 5) {
					this.save_04();
				}
				if (this.DataType == 35) {
					this.save_03();
				}
				if (this.DataType == 36) {
					this.save_04();
				}
				if (this.DataType == 37) {
					this.save_04();
				}
				if (this.DataType == 243) {
					this.save_03();
				}
				if (this.DataType != 244) {
					this.save_04();
				}
			}
		} catch (java.lang.Exception e) {
		}

	}
    
    public java.lang.String GetPointIDfromDeviceID(java.lang.String _DeviceID) {
        java.lang.String pid = "";     
        javagprssrv.DataBase.DBSQLExecute dbse = new javagprssrv.DataBase.DBSQLExecute();
        dbse.getPointIDByDeviceID(_DeviceID);
        return pid;
    }
    //数据库结构不符
    public void save_03() throws java.lang.Exception {
        this.ReadRecvDataGetValue();
        try {
            java.lang.String sql = "select count(*) from T_YSJ where DeviceID='" + this.YB_YBH + "' and dataYear='20" + this.s2_Year + "' and dataMonth='" + this.s2_Month + "'";
            final javagprssrv.DataBase.DBSQLExecute dbse = new javagprssrv.DataBase.DBSQLExecute();
            final java.sql.ResultSet rs = dbse.SQL_Query(0, sql);
            
            final int recordCounts = dbse.findCountYsj(this.YB_YBH , "20"+s2_Year, s2_Month);
            final java.lang.String _zyxs = "";
            final java.lang.String time = new javagprssrv.Common.DateTime().getDateTime("YYYY-MM-DD HH:NN:SS");
            final java.lang.String PID = this.GetPointIDfromDeviceID(this.YB_YBH);
            final java.lang.String s_zdz = java.lang.String.valueOf(this.szdz);
            final java.lang.String s_zxz = java.lang.String.valueOf(this.szxz);
            final java.lang.String s_pjz = java.lang.String.valueOf(this.spjz);
            this.zdzs = "20" + this.s2_Year + "-" + this.zdzs.substring(0, 2) + "-" + this.zdzs.substring(2, 4) + " " + this.zdzs.substring(4, 6) + ":" + this.zdzs.substring(6, 8) + ":00";
            this.zxzs = "20" + this.s2_Year + "-" + this.zxzs.substring(0, 2) + "-" + this.zxzs.substring(2, 4) + " " + this.zxzs.substring(4, 6) + ":" + this.zxzs.substring(6, 8) + ":00";
            if (recordCounts == 0) {
                sql = "insert into T_YSJ(dataYear,dataMonth,DeviceID,PointID,CreateTime,totalRunMin,AboveUpperLimitMin,underLowerLimitMin,maxVoltage,maxVoltageTime,minVoltage,minVoltageTime,avgVoltage) values (";
                sql = sql + "20" + this.s2_Year + "," + this.s2_Month + "," + this.YB_YBH + "," + PID + ",'" + time + "',";
                sql = sql + this.zyxs + "," + this.csxs + "," + this.cxxs + "," + s_zdz + ",'" + this.zdzs + "'," + s_zxz + ",'" + this.zxzs + "'," + s_pjz + ")";
            }
            else {
                sql = "UPDATE T_YSJ set createTime='" + time + "',totalRunMin=" + this.zyxs + ",AboveUpperLimitMin=" + this.csxs + ",underLowerLimitMin=" + this.cxxs + ",maxVoltage=" + s_zdz + ",maxVoltageTime='" + this.zdzs + "',minVoltage=" + s_zxz + ",minVoltageTime='" + this.zxzs + "',avgVoltage=" + s_pjz;
                sql = sql + " WHERE datayear=20" + this.s2_Year + " and dataMonth=" + this.s2_Month + " and deviceID=" + this.YBH;
            }
            this.SQLExecute(sql);
        }
        catch (java.lang.Exception ex) {}
    }
    
    public void save_04() throws java.lang.Exception {
        this.ReadRecvDataGetValue();
        try {
            //java.lang.String sql = "select count(*) from T_RSJ where DeviceID='" + this.YB_YBH + "' and dataYear='20" + this.s2_Year + "' and dataMonth='" + this.s2_Month + "' and dataDay='" + this.s2_Date + "'";
            String sql;
            final javagprssrv.DataBase.DBSQLExecute dbse = new javagprssrv.DataBase.DBSQLExecute();
           
            final int recordCounts = dbse.findCountRsj(this.YB_YBH , "20"+s2_Year, s2_Month, this.s2_Date);
            final java.lang.String _zyxs = "";
            final java.lang.String time = new javagprssrv.Common.DateTime().getDateTime("YYYY-MM-DD HH:NN:SS");
            final java.lang.String PID = this.GetPointIDfromDeviceID(this.YB_YBH);
            final java.lang.String s_zdz = java.lang.String.valueOf(this.szdz);
            final java.lang.String s_zxz = java.lang.String.valueOf(this.szxz);
            final java.lang.String s_pjz = java.lang.String.valueOf(this.spjz);
            this.zdzs = "20" + this.s2_Year + "-" + this.zdzs.substring(0, 2) + "-" + this.zdzs.substring(2, 4) + " " + this.zdzs.substring(4, 6) + ":" + this.zdzs.substring(6, 8) + ":00";
            this.zxzs = "20" + this.s2_Year + "-" + this.zxzs.substring(0, 2) + "-" + this.zxzs.substring(2, 4) + " " + this.zxzs.substring(4, 6) + ":" + this.zxzs.substring(6, 8) + ":00";
            if (recordCounts == 0) {
                sql = "insert into T_RSJ(dataYear,dataMonth,dataDay,DeviceID,PointID,CreateTime,totalRunMin,AboveUpperLimitMin,underLowerLimitMin,maxVoltage,maxVoltageTime,minVoltage,minVoltageTime,avgVoltage) values (";
                sql = sql + "20" + this.s2_Year + "," + this.s2_Month + "," + this.s2_Date + "," + this.YB_YBH + "," + PID + ",'" + time + "',";
                sql = sql + this.zyxs + "," + this.csxs + "," + this.cxxs + "," + s_zdz + ",'" + this.zdzs + "'," + s_zxz + ",'" + this.zxzs + "'," + s_pjz + ")";
            }
            else {
                sql = "UPDATE T_RSJ set createTime='" + time + "',totalRunMin=" + this.zyxs + ",AboveUpperLimitMin=" + this.csxs + ",underLowerLimitMin=" + this.cxxs + ",maxVoltage=" + s_zdz + ",maxVoltageTime='" + this.zdzs + "',minVoltage=" + s_zxz + ",minVoltageTime='" + this.zxzs + "',avgVoltage=" + s_pjz;
                sql = sql + " WHERE datayear=20" + this.s2_Year + " and dataMonth=" + this.s2_Month + " and dataDay=" + this.s2_Date + " and deviceID=" + this.YBH;
            }
            this.SQLExecute(sql);
            java.lang.Thread.sleep(500L);
            sql = "UPDATE T_RSJ SET ";
            sql = sql + "H00=" + this.sz00 + "," + "H01=" + this.sz01 + "," + "H02=" + this.sz02 + "," + "H03=" + this.sz03 + "," + "H04=" + this.sz04 + "," + "H05=" + this.sz05 + "," + "H06=" + this.sz06 + "," + "H07=" + this.sz07 + "," + "H08=" + this.sz08 + "," + "H09=" + this.sz09 + ",";
            sql = sql + "H10=" + this.sz10 + "," + "H11=" + this.sz11 + "," + "H12=" + this.sz12 + "," + "H13=" + this.sz13 + "," + "H14=" + this.sz14 + "," + "H15=" + this.sz15 + "," + "H16=" + this.sz16 + "," + "H17=" + this.sz17 + "," + "H18=" + this.sz18 + "," + "H19=" + this.sz19 + ",";
            sql = sql + "H20=" + this.sz20 + "," + "H21=" + this.sz21 + "," + "H22=" + this.sz22 + "," + "H23=" + this.sz23 + " WHERE datayear=20" + this.s2_Year + " and dataMonth=" + this.s2_Month + " and dataDay=" + this.s2_Date + " and deviceID=" + this.YBH;
            this.SQLExecute(sql);
        }
        catch (java.lang.Exception ex) {}
    }
    
    public boolean checkData() {
        boolean res = true;
        this.TermNo = this._Data.substring(1, 4);
        final int OffsetZ = this._Data.indexOf("Z");
        if (OffsetZ == -1) {
            res = false;
        }
        this.AsynTelNo = this._Data.substring(OffsetZ + 5, OffsetZ + 5 + 11).trim();
        if (this.AsynTelNo.length() != 11) {
            res = false;
        }
        if (!this.GetYBInfo()) {
            res = false;
        }
        return res;
    }
    
    public boolean GetYBInfo() {
        boolean res = true;
        //final java.lang.String sql = "select VL.code,d.uniqueCode,device.* from V_pointExtend device left outer join T_VoltageLevel VL on VL.id=device.VoltageLevelID left outer join T_Device d on d.id=device.DeviceId  where d.uniqueCode='" + this.AsynTelNo + "'";
        final javagprssrv.DataBase.DBSQLExecute dbse = new javagprssrv.DataBase.DBSQLExecute();
        Map<String,Object> map = dbse.findYbInfo(this.AsynTelNo);
        try {
            
            this.YB_YBH = String.valueOf( map.get("deviceId"));
            this.YBH = this.YB_YBH;
            final int code =Integer.parseInt((String)map.get("code"));
            this.YB_BL = 1.0;
            if (code == 2) {
                this.YB_BL = 0.1;
            }
            if (code == 3) {
                this.YB_BL = 0.1;
            }
            if (code == 7) {
                this.YB_BL = 1.0;
            }
            if (code == 8) {
                this.YB_BL = 10.0;
            }
            if (code == 9) {
                this.YB_BL = 10.0;
            }
            if (code == 10) {
                this.YB_BL = 10.0;
            }
            if (code == 12) {
                this.YB_BL = 100.0;
            }
            if (code == 13) {
                this.YB_BL = 100.0;
            }
            if (code == 14) {
                this.YB_BL = 100.0;
            }
            if (code == 15) {
                this.YB_BL = 100.0;
            }
            if (code == 16) {
                this.YB_BL = 100.0;
            }
            if (code == 17) {
                this.YB_BL = 1000.0;
            }
            this.YB_BBXSW = 3;
            this.YB_floatGS = "0.";
            for (int i = 0; i < this.YB_BBXSW; ++i) {
                this.YB_floatGS += "0";
            }
            res = true;
        }
        catch (java.lang.Exception e) {
            res = false;
        }
        return res;
    }
    
    public void ReadRecvDataGetValue() throws java.lang.Exception {
        if (this.DataType == 3 || this.DataType == 4 || this.DataType == 5 || this.DataType == 35 || this.DataType == 36 || this.DataType == 37 || this.DataType == 243 || this.DataType == 244) {
            this.s2_Year = this._Data.substring(9, 11);
            this.s2_Month = this._Data.substring(11, 13);
            this.s2_Date = this._Data.substring(13, 15);
            final int offset = 9;
            this.zyxs = this._Data.substring(6 + offset, 6 + offset + 5);
            this.csxs = this._Data.substring(11 + offset, 11 + offset + 5);
            this.cxxs = this._Data.substring(16 + offset, 16 + offset + 5);
            this.hgs = java.lang.String.valueOf(java.lang.Integer.valueOf(this.zyxs) - java.lang.Integer.valueOf(this.csxs) - java.lang.Integer.valueOf(this.cxxs));
            try {
                this.zdz = java.lang.Float.valueOf(this._Data.substring(21 + offset, 21 + offset + 4));
            }
            catch (java.lang.Exception e) {
                this.zdz = 0.0;
            }
            this.zdzs = this._Data.substring(25 + offset, 25 + offset + 8);
            try {
                this.zxz = java.lang.Float.valueOf(this._Data.substring(33 + offset, 33 + offset + 4));
            }
            catch (java.lang.Exception e) {
                this.zxz = 9999.0;
            }
            this.zxzs = this._Data.substring(37 + offset, 37 + offset + 8);
            try {
                this.pjz = java.lang.Float.valueOf(this._Data.substring(45 + offset, 45 + offset + 4));
            }
            catch (java.lang.Exception e) {
                this.pjz = 0.0;
            }
            this.z00 = java.lang.Float.valueOf(this._Data.substring(49 + offset, 49 + offset + 4));
            this.z01 = java.lang.Float.valueOf(this._Data.substring(53 + offset, 53 + offset + 4));
            this.z02 = java.lang.Float.valueOf(this._Data.substring(57 + offset, 57 + offset + 4));
            this.z03 = java.lang.Float.valueOf(this._Data.substring(61 + offset, 61 + offset + 4));
            this.z04 = java.lang.Float.valueOf(this._Data.substring(65 + offset, 65 + offset + 4));
            this.z05 = java.lang.Float.valueOf(this._Data.substring(69 + offset, 69 + offset + 4));
            this.z06 = java.lang.Float.valueOf(this._Data.substring(73 + offset, 73 + offset + 4));
            this.z07 = java.lang.Float.valueOf(this._Data.substring(77 + offset, 77 + offset + 4));
            this.z08 = java.lang.Float.valueOf(this._Data.substring(81 + offset, 81 + offset + 4));
            this.z09 = java.lang.Float.valueOf(this._Data.substring(85 + offset, 85 + offset + 4));
            this.z10 = java.lang.Float.valueOf(this._Data.substring(89 + offset, 89 + offset + 4));
            this.z11 = java.lang.Float.valueOf(this._Data.substring(93 + offset, 93 + offset + 4));
            this.z12 = java.lang.Float.valueOf(this._Data.substring(97 + offset, 97 + offset + 4));
            this.z13 = java.lang.Float.valueOf(this._Data.substring(101 + offset, 101 + offset + 4));
            this.z14 = java.lang.Float.valueOf(this._Data.substring(105 + offset, 105 + offset + 4));
            this.z15 = java.lang.Float.valueOf(this._Data.substring(109 + offset, 109 + offset + 4));
            this.z16 = java.lang.Float.valueOf(this._Data.substring(113 + offset, 113 + offset + 4));
            this.z17 = java.lang.Float.valueOf(this._Data.substring(117 + offset, 117 + offset + 4));
            this.z18 = java.lang.Float.valueOf(this._Data.substring(121 + offset, 121 + offset + 4));
            this.z19 = java.lang.Float.valueOf(this._Data.substring(125 + offset, 125 + offset + 4));
            this.z20 = java.lang.Float.valueOf(this._Data.substring(129 + offset, 129 + offset + 4));
            this.z21 = java.lang.Float.valueOf(this._Data.substring(133 + offset, 133 + offset + 4));
            this.z22 = java.lang.Float.valueOf(this._Data.substring(137 + offset, 137 + offset + 4));
            this.z23 = java.lang.Float.valueOf(this._Data.substring(141 + offset, 141 + offset + 4));
            final java.text.DecimalFormat df = new java.text.DecimalFormat("0.0000");
            this.szdz = df.format(this.zdz * this.YB_BL);
            this.szxz = df.format(this.zxz * this.YB_BL);
            this.spjz = df.format(this.pjz * this.YB_BL);
            this.sz00 = df.format(this.z00 * this.YB_BL);
            this.sz01 = df.format(this.z01 * this.YB_BL);
            this.sz02 = df.format(this.z02 * this.YB_BL);
            this.sz03 = df.format(this.z03 * this.YB_BL);
            this.sz04 = df.format(this.z04 * this.YB_BL);
            this.sz05 = df.format(this.z05 * this.YB_BL);
            this.sz06 = df.format(this.z06 * this.YB_BL);
            this.sz07 = df.format(this.z07 * this.YB_BL);
            this.sz08 = df.format(this.z08 * this.YB_BL);
            this.sz09 = df.format(this.z09 * this.YB_BL);
            this.sz10 = df.format(this.z10 * this.YB_BL);
            this.sz11 = df.format(this.z11 * this.YB_BL);
            this.sz12 = df.format(this.z12 * this.YB_BL);
            this.sz13 = df.format(this.z13 * this.YB_BL);
            this.sz14 = df.format(this.z14 * this.YB_BL);
            this.sz15 = df.format(this.z15 * this.YB_BL);
            this.sz16 = df.format(this.z16 * this.YB_BL);
            this.sz17 = df.format(this.z17 * this.YB_BL);
            this.sz18 = df.format(this.z18 * this.YB_BL);
            this.sz19 = df.format(this.z19 * this.YB_BL);
            this.sz20 = df.format(this.z20 * this.YB_BL);
            this.sz21 = df.format(this.z21 * this.YB_BL);
            this.sz22 = df.format(this.z22 * this.YB_BL);
            this.sz23 = df.format(this.z23 * this.YB_BL);
        }
    }
    
    public void SQLExecute(final java.lang.String sql) {
        synchronized (javagprssrv.JGS_Param.UnSavedDBSQLString) {
            javagprssrv.JGS_Param.UnSavedDBSQLString.add(sql);
        }
    }
}
